package com.yunxi.poc.starbucks.service.impl;

import com.yunxi.poc.starbucks.common.BizConstant;
import com.yunxi.poc.starbucks.entity.Tb1Store;
import com.yunxi.poc.starbucks.service.IPunishmentService;
import com.yunxi.poc.starbucks.service.ITStoreCapacityService;
import com.yunxi.poc.starbucks.service.ITb1TransactionSaleService;
import com.yunxi.poc.starbucks.vo.StoreRateVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PunishmentServiceImpl implements IPunishmentService {

    @Resource
    ITb1TransactionSaleService tb1TransactionSaleService;

    @Resource
    ITStoreCapacityService storeCapacityService;

    @Override
    public double scorePunishment(LocalDateTime current, Tb1Store store, List<Tb1Store> stores, StoreRateVO calRes) {
        // 最近 24h 7d 咖啡数量
        double last24hoursCoffeeCount = tb1TransactionSaleService.calculateSales(
                Arrays.asList(Integer.valueOf(store.getGlobalStoreNumber())),
                current.minusSeconds(BizConstant.PUNISHMENT_MINUS_SECS),
                current);
        double last7daysCoffeeCount = storeCapacityService.calculateSales(
                Arrays.asList(Integer.valueOf(store.getGlobalStoreNumber())),
                current.toLocalDate().minusDays(BizConstant.PUNISHMENT_MINUS_DAYS),
                current.toLocalDate());

        List<Integer> storeIds = stores.stream()
                .map(e -> Integer.valueOf(e.getGlobalStoreNumber()))
                .distinct()
                .collect(Collectors.toList());
        storeIds.remove(Integer.valueOf(store.getGlobalStoreNumber()));

        // 最近 24h 7d 咖啡数量，及咖啡数量(最近其他门店的)
        double otherShopLast24hCoffeeCount = storeIds.size() == 0 ? 0 : tb1TransactionSaleService.calculateSales(storeIds, current.minusSeconds(BizConstant.PUNISHMENT_MINUS_SECS), current);
        double otherShopLast7daysCoffeeCount = storeIds.size() == 0 ? 0 : storeCapacityService.calculateSales(storeIds, current.toLocalDate().minusDays(BizConstant.PUNISHMENT_MINUS_DAYS), current.toLocalDate());

        // 24h 7d 的销量占比
        double last24hPercent = (otherShopLast24hCoffeeCount + last24hoursCoffeeCount) == 0
                ? 0 : last24hoursCoffeeCount / (otherShopLast24hCoffeeCount + last24hoursCoffeeCount);
        double last7daysPercent = (otherShopLast7daysCoffeeCount + last7daysCoffeeCount) == 0
                ? 0 : last7daysCoffeeCount / (otherShopLast7daysCoffeeCount + last7daysCoffeeCount);

        // -max(1/2 - 24h以内的销量占比 ÷ 7天内销量占比, 0)
        double rate = Math.max(
                BizConstant.PUNISHMENT_RATE_STANDARD - (last7daysPercent == 0 ? 0 : last24hPercent / last7daysPercent),
                0);

        calRes.setPunishmentLast24hCount(String.valueOf(last24hoursCoffeeCount));
        calRes.setPunishmentLast7daysCount(String.valueOf(last7daysCoffeeCount));
        calRes.setPunishmentLast24hOtherCount(String.valueOf(otherShopLast24hCoffeeCount));
        calRes.setPunishmentLast7daysOtherCount(String.valueOf(otherShopLast7daysCoffeeCount));
        calRes.setPunishmentLast24hPercent(String.valueOf(last24hPercent));
        calRes.setPunishmentLast7daysPercent(String.valueOf(last7daysPercent));

        return -1 * rate;
    }
}